package com.tocean.demo.service;

import com.tocean.demo.entity.Attr.MyQuestion;
import com.tocean.demo.entity.questionbank.ExamId;
import com.tocean.demo.entity.questionbank.Examination;
import com.tocean.demo.repository.ExamIdRepository;
import com.tocean.demo.repository.ExaminationRepository;
import com.tocean.demo.util.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

@Service
public class ExaminationService {

    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    @Autowired
    private ExaminationRepository examinationRepository;

    @Autowired
    private ExamIdRepository examid;


    /**
     * 向数据库添加考试试卷
     * @param myquestion
     */
    @Transactional
    public void AddExam(List<MyQuestion> myquestion){
        Examination examination = new Examination();


        List<Examination> examination1 = examinationRepository.findAllByTid(myquestion.get(0).getTid());
        if(examination1.size() == 0) {
            for (int i = 0; i < myquestion.size(); i++) {
                examination.setId(snowflakeIdWorker.nextId());
                examination.setTid(myquestion.get(i).getTid());
                examination.setCid(myquestion.get(i).getId());
                examination.setIndexs(myquestion.get(i).getIndexs());
                examination.setContent(myquestion.get(i).getContent());
                StringBuilder str = new StringBuilder();
                for (int m = 0; m < myquestion.get(i).getAnswer().size(); m++) {
                    if (m != myquestion.get(i).getAnswer().size() - 1)
                        str.append(myquestion.get(i).getAnswer().get(m) + "|");
                    else
                        str.append(myquestion.get(i).getAnswer().get(m));
                }
                examination.setAnswer(str.toString());
                examination.setTag_Name(setTag(myquestion.get(i).getTag_Name()));
                examinationRepository.save(examination);
            }
        }

        if(!examid.findById(myquestion.get(0).getTid()).isPresent()){
            ExamId ex = new ExamId();
            ex.setId(myquestion.get(0).getTid());
            examid.save(ex);
        }
    }

    /**
     * 只获取  年级+科目 标签
     * @param tagName
     * @return
     */
    public String setTag(String tagName){
            int index = tagName.indexOf("年");
            String mid = tagName.substring(index - 1, index + 2);
            StringBuilder sb = new StringBuilder();
            if (tagName.indexOf("语文") != -1) {
                sb.append(mid);
                sb.append("语文");
            } else if (tagName.indexOf("数学") != -1) {
                sb.append(mid);
                sb.append("数学");
            } else if (tagName.indexOf("英语") != -1) {
                sb.append(mid);
                sb.append("英语");
            } else if (tagName.indexOf("物理") != -1) {
                sb.append(mid);
                sb.append("物理");
            } else if (tagName.indexOf("化学") != -1) {
                sb.append(mid);
                sb.append("化学");
            } else if (tagName.indexOf("知识竞赛") != -1) {
                sb.append(mid);
                sb.append("知识竞赛");
            }


        return sb.toString();
    }
}
